{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "超参数选择.ipynb",
      "provenance": [],
      "toc_visible": true,
      "authorship_tag": "ABX9TyO3VwRYmoHueJWQi4n8LQmI",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/sunyingjian/AI-in-well-logging/blob/master/%E8%B6%85%E5%8F%82%E6%95%B0%E9%80%89%E6%8B%A9.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Cqy3vXXOMI06",
        "colab_type": "text"
      },
      "source": [
        "### <font size=5px color=\"red\">✦ *Google Colab 突破90分钟自动断开:</font>\n",
        "<p><font size=3px > 每60分钟自动运行代码以刷新90分钟断开限制. 打开 developer-settings (在你的浏览器) 快速健 Ctrl+Shift+I 然后按console 输入以下代码 Enter. ( mac 按 Option+Command+I)</p><b>复制以下隐藏代码粉贴在浏览器console！！不要关闭浏览器以免失效</b>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3Z8cK8h2Avbv",
        "colab_type": "text"
      },
      "source": [
        "<code>function ClickConnect(){\n",
        "console.log(\"Working\"); \n",
        "document.querySelector(\"colab-connect-button\").click() \n",
        "}setInterval(ClickConnect,6000)</code>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jtClEMAMLVHw",
        "colab_type": "code",
        "cellView": "both",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "0d7aa806-3058-483c-b51b-ca56ac2ae76b"
      },
      "source": [
        "#@markdown <h3>← 输入了代码后运行以防止断开</h>\n",
        "\n",
        "\n",
        "import IPython\n",
        "from google.colab import output\n",
        "\n",
        "display(IPython.display.Javascript('''\n",
        " function ClickConnect(){\n",
        "   btn = document.querySelector(\"colab-connect-button\")\n",
        "   if (btn != null){\n",
        "     console.log(\"Click colab-connect-button\"); \n",
        "     btn.click() \n",
        "     }\n",
        "   \n",
        "   btn = document.getElementById('ok')\n",
        "   if (btn != null){\n",
        "     console.log(\"Click reconnect\"); \n",
        "     btn.click() \n",
        "     }\n",
        "  }\n",
        "  \n",
        "setInterval(ClickConnect,60000)\n",
        "'''))\n",
        "\n",
        "print(\"Done.\")"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/javascript": [
              "\n",
              " function ClickConnect(){\n",
              "   btn = document.querySelector(\"colab-connect-button\")\n",
              "   if (btn != null){\n",
              "     console.log(\"Click colab-connect-button\"); \n",
              "     btn.click() \n",
              "     }\n",
              "   \n",
              "   btn = document.getElementById('ok')\n",
              "   if (btn != null){\n",
              "     console.log(\"Click reconnect\"); \n",
              "     btn.click() \n",
              "     }\n",
              "  }\n",
              "  \n",
              "setInterval(ClickConnect,60000)\n"
            ],
            "text/plain": [
              "<IPython.core.display.Javascript object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1vII9VJFF-tm",
        "colab_type": "text"
      },
      "source": [
        "#准备数据"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ezkhBvSCGBan",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 311
        },
        "outputId": "c396d615-de35-4999-9ace-3dd7640d47b7"
      },
      "source": [
        "! git clone https://github.com/seg/tutorials-2016.git\n",
        "! git clone https://github.com/sunyingjian/numpy-.git\n",
        "! git clone https://github.com/sunyingjian/Machine-Learning-From-Scratch.git"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Cloning into 'tutorials-2016'...\n",
            "remote: Enumerating objects: 161, done.\u001b[K\n",
            "remote: Total 161 (delta 0), reused 0 (delta 0), pack-reused 161\u001b[K\n",
            "Receiving objects: 100% (161/161), 16.86 MiB | 25.06 MiB/s, done.\n",
            "Resolving deltas: 100% (64/64), done.\n",
            "Cloning into 'numpy-'...\n",
            "remote: Enumerating objects: 38, done.\u001b[K\n",
            "remote: Counting objects: 100% (38/38), done.\u001b[K\n",
            "remote: Compressing objects: 100% (38/38), done.\u001b[K\n",
            "remote: Total 127 (delta 7), reused 0 (delta 0), pack-reused 89\u001b[K\n",
            "Receiving objects: 100% (127/127), 4.22 MiB | 21.61 MiB/s, done.\n",
            "Resolving deltas: 100% (40/40), done.\n",
            "Cloning into 'Machine-Learning-From-Scratch'...\n",
            "remote: Enumerating objects: 287, done.\u001b[K\n",
            "remote: Total 287 (delta 0), reused 0 (delta 0), pack-reused 287\u001b[K\n",
            "Receiving objects: 100% (287/287), 91.24 KiB | 4.15 MiB/s, done.\n",
            "Resolving deltas: 100% (133/133), done.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oJazUyxIGH51",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 407
        },
        "outputId": "aa3c1142-0419-487a-f2f7-7247a3e7dbf7"
      },
      "source": [
        "%matplotlib inline\n",
        "#%matplotlib inline 可以在Ipython编译器里直接使用，功能是可以内嵌绘图，并且可以省略掉plt.show()这一步。\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "import matplotlib as mpl\n",
        "import matplotlib.pyplot as plt\n",
        "import matplotlib.colors as colors\n",
        "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
        "from pandas import set_option\n",
        "set_option(\"display.max_rows\", 10)#设置要显示的默认行数，显示的最大行数是10\n",
        "pd.options.mode.chained_assignment = None #为了在增加列表行数的时候防止出现setting with copy warning\n",
        "filename = 'facies_vectors.csv'\n",
        "training_data = pd.read_csv('/content/tutorials-2016/1610_Facies_classification/training_data.csv')\n",
        "training_data"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Facies</th>\n",
              "      <th>Formation</th>\n",
              "      <th>Well Name</th>\n",
              "      <th>Depth</th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>3</td>\n",
              "      <td>A1 SH</td>\n",
              "      <td>SHRIMPLIN</td>\n",
              "      <td>2793.0</td>\n",
              "      <td>77.450</td>\n",
              "      <td>0.664</td>\n",
              "      <td>9.900</td>\n",
              "      <td>11.915</td>\n",
              "      <td>4.600</td>\n",
              "      <td>1</td>\n",
              "      <td>1.000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>3</td>\n",
              "      <td>A1 SH</td>\n",
              "      <td>SHRIMPLIN</td>\n",
              "      <td>2793.5</td>\n",
              "      <td>78.260</td>\n",
              "      <td>0.661</td>\n",
              "      <td>14.200</td>\n",
              "      <td>12.565</td>\n",
              "      <td>4.100</td>\n",
              "      <td>1</td>\n",
              "      <td>0.979</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>3</td>\n",
              "      <td>A1 SH</td>\n",
              "      <td>SHRIMPLIN</td>\n",
              "      <td>2794.0</td>\n",
              "      <td>79.050</td>\n",
              "      <td>0.658</td>\n",
              "      <td>14.800</td>\n",
              "      <td>13.050</td>\n",
              "      <td>3.600</td>\n",
              "      <td>1</td>\n",
              "      <td>0.957</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>3</td>\n",
              "      <td>A1 SH</td>\n",
              "      <td>SHRIMPLIN</td>\n",
              "      <td>2794.5</td>\n",
              "      <td>86.100</td>\n",
              "      <td>0.655</td>\n",
              "      <td>13.900</td>\n",
              "      <td>13.115</td>\n",
              "      <td>3.500</td>\n",
              "      <td>1</td>\n",
              "      <td>0.936</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>3</td>\n",
              "      <td>A1 SH</td>\n",
              "      <td>SHRIMPLIN</td>\n",
              "      <td>2795.0</td>\n",
              "      <td>74.580</td>\n",
              "      <td>0.647</td>\n",
              "      <td>13.500</td>\n",
              "      <td>13.300</td>\n",
              "      <td>3.400</td>\n",
              "      <td>1</td>\n",
              "      <td>0.915</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3227</th>\n",
              "      <td>5</td>\n",
              "      <td>C LM</td>\n",
              "      <td>CHURCHMAN BIBLE</td>\n",
              "      <td>3120.5</td>\n",
              "      <td>46.719</td>\n",
              "      <td>0.947</td>\n",
              "      <td>1.828</td>\n",
              "      <td>7.254</td>\n",
              "      <td>3.617</td>\n",
              "      <td>2</td>\n",
              "      <td>0.685</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3228</th>\n",
              "      <td>5</td>\n",
              "      <td>C LM</td>\n",
              "      <td>CHURCHMAN BIBLE</td>\n",
              "      <td>3121.0</td>\n",
              "      <td>44.563</td>\n",
              "      <td>0.953</td>\n",
              "      <td>2.241</td>\n",
              "      <td>8.013</td>\n",
              "      <td>3.344</td>\n",
              "      <td>2</td>\n",
              "      <td>0.677</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3229</th>\n",
              "      <td>5</td>\n",
              "      <td>C LM</td>\n",
              "      <td>CHURCHMAN BIBLE</td>\n",
              "      <td>3121.5</td>\n",
              "      <td>49.719</td>\n",
              "      <td>0.964</td>\n",
              "      <td>2.925</td>\n",
              "      <td>8.013</td>\n",
              "      <td>3.190</td>\n",
              "      <td>2</td>\n",
              "      <td>0.669</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3230</th>\n",
              "      <td>5</td>\n",
              "      <td>C LM</td>\n",
              "      <td>CHURCHMAN BIBLE</td>\n",
              "      <td>3122.0</td>\n",
              "      <td>51.469</td>\n",
              "      <td>0.965</td>\n",
              "      <td>3.083</td>\n",
              "      <td>7.708</td>\n",
              "      <td>3.152</td>\n",
              "      <td>2</td>\n",
              "      <td>0.661</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3231</th>\n",
              "      <td>5</td>\n",
              "      <td>C LM</td>\n",
              "      <td>CHURCHMAN BIBLE</td>\n",
              "      <td>3122.5</td>\n",
              "      <td>50.031</td>\n",
              "      <td>0.970</td>\n",
              "      <td>2.609</td>\n",
              "      <td>6.668</td>\n",
              "      <td>3.295</td>\n",
              "      <td>2</td>\n",
              "      <td>0.653</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>3232 rows × 11 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "      Facies Formation        Well Name   Depth  ...   PHIND     PE  NM_M  RELPOS\n",
              "0          3     A1 SH        SHRIMPLIN  2793.0  ...  11.915  4.600     1   1.000\n",
              "1          3     A1 SH        SHRIMPLIN  2793.5  ...  12.565  4.100     1   0.979\n",
              "2          3     A1 SH        SHRIMPLIN  2794.0  ...  13.050  3.600     1   0.957\n",
              "3          3     A1 SH        SHRIMPLIN  2794.5  ...  13.115  3.500     1   0.936\n",
              "4          3     A1 SH        SHRIMPLIN  2795.0  ...  13.300  3.400     1   0.915\n",
              "...      ...       ...              ...     ...  ...     ...    ...   ...     ...\n",
              "3227       5      C LM  CHURCHMAN BIBLE  3120.5  ...   7.254  3.617     2   0.685\n",
              "3228       5      C LM  CHURCHMAN BIBLE  3121.0  ...   8.013  3.344     2   0.677\n",
              "3229       5      C LM  CHURCHMAN BIBLE  3121.5  ...   8.013  3.190     2   0.669\n",
              "3230       5      C LM  CHURCHMAN BIBLE  3122.0  ...   7.708  3.152     2   0.661\n",
              "3231       5      C LM  CHURCHMAN BIBLE  3122.5  ...   6.668  3.295     2   0.653\n",
              "\n",
              "[3232 rows x 11 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NNi8cT_FGK4C",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 69
        },
        "outputId": "85247374-2641-435e-e226-ef1cf6f983c7"
      },
      "source": [
        "training_data['Well Name'] = training_data['Well Name'].astype('category')\n",
        "training_data['Formation'] = training_data['Formation'].astype('category')\n",
        "training_data['Well Name'].unique()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[SHRIMPLIN, SHANKLE, LUKE G U, CROSS H CATTLE, NOLAN, Recruit F9, NEWBY, CHURCHMAN BIBLE]\n",
              "Categories (8, object): [SHRIMPLIN, SHANKLE, LUKE G U, CROSS H CATTLE, NOLAN, Recruit F9, NEWBY,\n",
              "                         CHURCHMAN BIBLE]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hdFLXVLvGQSv",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 288
        },
        "outputId": "61da875e-c527-41c0-d719-436a739a3fda"
      },
      "source": [
        "correct_facies_labels = training_data['Facies'].values\n",
        "\n",
        "feature_vectors = training_data.drop(['Formation', 'Well Name', 'Depth','Facies'], axis=1)\n",
        "feature_vectors.describe()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>GR</th>\n",
              "      <th>ILD_log10</th>\n",
              "      <th>DeltaPHI</th>\n",
              "      <th>PHIND</th>\n",
              "      <th>PE</th>\n",
              "      <th>NM_M</th>\n",
              "      <th>RELPOS</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>count</th>\n",
              "      <td>3232.000000</td>\n",
              "      <td>3232.000000</td>\n",
              "      <td>3232.000000</td>\n",
              "      <td>3232.000000</td>\n",
              "      <td>3232.000000</td>\n",
              "      <td>3232.000000</td>\n",
              "      <td>3232.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mean</th>\n",
              "      <td>66.135769</td>\n",
              "      <td>0.642719</td>\n",
              "      <td>3.559642</td>\n",
              "      <td>13.483213</td>\n",
              "      <td>3.725014</td>\n",
              "      <td>1.498453</td>\n",
              "      <td>0.520287</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>std</th>\n",
              "      <td>30.854826</td>\n",
              "      <td>0.241845</td>\n",
              "      <td>5.228948</td>\n",
              "      <td>7.698980</td>\n",
              "      <td>0.896152</td>\n",
              "      <td>0.500075</td>\n",
              "      <td>0.286792</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>min</th>\n",
              "      <td>13.250000</td>\n",
              "      <td>-0.025949</td>\n",
              "      <td>-21.832000</td>\n",
              "      <td>0.550000</td>\n",
              "      <td>0.200000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.010000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>25%</th>\n",
              "      <td>46.918750</td>\n",
              "      <td>0.492750</td>\n",
              "      <td>1.163750</td>\n",
              "      <td>8.346750</td>\n",
              "      <td>3.100000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.273000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>50%</th>\n",
              "      <td>65.721500</td>\n",
              "      <td>0.624437</td>\n",
              "      <td>3.500000</td>\n",
              "      <td>12.150000</td>\n",
              "      <td>3.551500</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.526000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>75%</th>\n",
              "      <td>79.626250</td>\n",
              "      <td>0.812735</td>\n",
              "      <td>6.432500</td>\n",
              "      <td>16.453750</td>\n",
              "      <td>4.300000</td>\n",
              "      <td>2.000000</td>\n",
              "      <td>0.767250</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>max</th>\n",
              "      <td>361.150000</td>\n",
              "      <td>1.480000</td>\n",
              "      <td>18.600000</td>\n",
              "      <td>84.400000</td>\n",
              "      <td>8.094000</td>\n",
              "      <td>2.000000</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "                GR    ILD_log10  ...         NM_M       RELPOS\n",
              "count  3232.000000  3232.000000  ...  3232.000000  3232.000000\n",
              "mean     66.135769     0.642719  ...     1.498453     0.520287\n",
              "std      30.854826     0.241845  ...     0.500075     0.286792\n",
              "min      13.250000    -0.025949  ...     1.000000     0.010000\n",
              "25%      46.918750     0.492750  ...     1.000000     0.273000\n",
              "50%      65.721500     0.624437  ...     1.000000     0.526000\n",
              "75%      79.626250     0.812735  ...     2.000000     0.767250\n",
              "max     361.150000     1.480000  ...     2.000000     1.000000\n",
              "\n",
              "[8 rows x 7 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "go4u4F6TGzF4",
        "colab_type": "text"
      },
      "source": [
        "##通过sklearn预置模块实现标准化"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EBHLR9G2GsVi",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from sklearn import preprocessing\n",
        "\n",
        "scaler = preprocessing.StandardScaler().fit(feature_vectors)\n",
        "scaled_features = scaler.transform(feature_vectors)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_luefAEZG2mB",
        "colab_type": "text"
      },
      "source": [
        "##分割数据"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NP8U4FhEG0AO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from sklearn.model_selection import train_test_split\n",
        "\n",
        "X_train, X_test, y_train, y_test = train_test_split(\n",
        "        scaled_features, correct_facies_labels, test_size=0.2, random_state=42)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DRvMX_szHRSo",
        "colab_type": "text"
      },
      "source": [
        "#用knn分类"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "14F4LQ7HHT06",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 86
        },
        "outputId": "84398beb-5222-441f-f70d-d8d1e9d6c319"
      },
      "source": [
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "from sklearn import metrics\n",
        "neight = KNeighborsClassifier(n_neighbors=6)\n",
        "neight.fit(X_train,y_train)\n",
        "print(neight)\n",
        "ypredrf1 = neight.predict(X_test)\n",
        "print('Accuracy : %f' % (metrics.accuracy_score(y_test, ypredrf1)))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
            "                     metric_params=None, n_jobs=None, n_neighbors=6, p=2,\n",
            "                     weights='uniform')\n",
            "Accuracy : 0.695518\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_ITQVlEVRQq8",
        "colab_type": "text"
      },
      "source": [
        "#xgboost"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GKMA-VsxIR11",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "a4e0b0eb-7bcc-4532-f042-5f5338593fb8"
      },
      "source": [
        "from xgboost import plot_importance\n",
        "from matplotlib import pyplot as plt\n",
        "\n",
        "import xgboost as xgb\n",
        "from numpy import loadtxt\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import accuracy_score\n",
        "\n",
        "params ={'learning_rate': 0.4,\n",
        "          'max_depth': 20,                # 构建树的深度，越大越容易过拟合\n",
        "          'num_boost_round':2000,\n",
        "          'objective': 'multi:softprob', # 多分类的问题\n",
        "          'random_state': 7,\n",
        "          'silent':0,\n",
        "          'num_class':10,                 # 类别数，与 multisoftmax 并用\n",
        "          'eta':0.8                      #为了防止过拟合，更新过程中用到的收缩步长。eta通过缩减特征 的权重使提升计算过程更加保守。缺省值为0.3，取值范围为：[0,1]\n",
        "        }\n",
        "model = xgb.train(params,xgb.DMatrix(X_train, y_train))\n",
        "y_pred=model.predict(xgb.DMatrix(X_test))\n",
        "\n",
        "model.save_model('testXGboostClass.model')  # 保存训练模型\n",
        "\n",
        "yprob = np.argmax(y_pred, axis=1)  # return the index of the biggest pro\n",
        "\n",
        "predictions = [round(value) for value in yprob]\n",
        "\n",
        "# evaluate predictions\n",
        "accuracy = accuracy_score(y_test, predictions)\n",
        "print(\"Accuracy: %.2f%%\" % (accuracy * 100.0))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Accuracy: 71.25%\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4EIYujJlIMhT",
        "colab_type": "text"
      },
      "source": [
        "#超参数调整"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8PVMYQJSLyBh",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 121
        },
        "outputId": "3ad00e35-8050-48cf-bf5d-10969531338e"
      },
      "source": [
        "pip install scikit-opt"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting scikit-opt\n",
            "  Downloading https://files.pythonhosted.org/packages/07/6a/5788e4b67dead680c38b908fa7f30251231e868b0a8eb1eb157425a219cd/scikit_opt-0.5.7-py3-none-any.whl\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from scikit-opt) (1.18.5)\n",
            "Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from scikit-opt) (1.4.1)\n",
            "Installing collected packages: scikit-opt\n",
            "Successfully installed scikit-opt-0.5.7\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VA6Nh7mKRUSs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 368
        },
        "outputId": "bff3d4cd-0423-4f8c-a42f-b4e73ccf72c9"
      },
      "source": [
        "from sko.GA import GA\n",
        "ga = GA(func=model, n_dim=6, size_pop=50, max_iter=800,precision=1e-7)\n",
        "best_x, best_y = ga.run()\n",
        "print('best_x:', best_x, '\\n', 'best_y:', best_y)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "error",
          "ename": "AttributeError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-19-d7274a114bb1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0msko\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mGA\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mGA\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mga\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mGA\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_dim\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize_pop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m50\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_iter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m800\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mprecision\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1e-7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mbest_x\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbest_y\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mga\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'best_x:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbest_x\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'best_y:'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbest_y\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/sko/GA.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, func, n_dim, size_pop, max_iter, prob_mut, lb, ub, constraint_eq, constraint_ueq, precision)\u001b[0m\n\u001b[1;32m    140\u001b[0m                  \u001b[0mconstraint_eq\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconstraint_ueq\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtuple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    141\u001b[0m                  precision=1e-7):\n\u001b[0;32m--> 142\u001b[0;31m         \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_dim\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize_pop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_iter\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprob_mut\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconstraint_eq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconstraint_ueq\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    143\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    144\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlb\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mub\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mones\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mn_dim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mub\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mones\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mn_dim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/sko/GA.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, func, n_dim, size_pop, max_iter, prob_mut, constraint_eq, constraint_ueq)\u001b[0m\n\u001b[1;32m     16\u001b[0m                  \u001b[0msize_pop\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m50\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax_iter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m200\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprob_mut\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.001\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m                  constraint_eq=tuple(), constraint_ueq=tuple()):\n\u001b[0;32m---> 18\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc_transformer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     19\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize_pop\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msize_pop\u001b[0m  \u001b[0;31m# size of population\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     20\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax_iter\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmax_iter\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/sko/tools.py\u001b[0m in \u001b[0;36mfunc_transformer\u001b[0;34m(func)\u001b[0m\n\u001b[1;32m     31\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     32\u001b[0m     \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 33\u001b[0;31m         \u001b[0;32mif\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__code__\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mco_argcount\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     34\u001b[0m             \u001b[0;32mdef\u001b[0m \u001b[0mfunc_transformed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     35\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mX\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mAttributeError\u001b[0m: 'Booster' object has no attribute '__code__'"
          ]
        }
      ]
    }
  ]
}